/**=========================================================
 * Directive: ztree.js
 * Initializes the ztree plugin
 =========================================================*/

App.directive('ztree', ['Utils',
    function(utils) {
        'use strict';
        
        return {
            restrict: 'EA',
            replace: true,
            template: function($element,$attrs) {
                return '<ul id="tree' + (Math.random() * 1000).toFixed(0) + '" class="ztree"></ul>'
            },
            scope: {
                option: '='
            },
            link: function ($scope, $element, $attrs) {
                //监听option变化
                $scope.$watch("option", 
                    function (newVal, oldVal) {
                        if (!utils.isEmpty(newVal)){
                            var scope = $scope.$parent;
                            if (!utils.isEmpty($attrs.level)){
                                for (var i = 1; i < $attrs.level; i++){
                                    scope = scope.$parent;
                                }
                            }
                            scope[newVal.name] = $.fn.zTree.init($element, newVal.setting, newVal.data);
                        }
                    }
                );            
            }
        }
    }
]);